package StudyCode.summary.Basic.mylong;

import java.math.BigDecimal;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class LongToFileSize {

	private static final long SIZE_BT = 1024L;
	private static final long SIZE_KB = SIZE_BT * 1024L;
	private static final long SIZE_MB = SIZE_KB * 1024L;
	private static final long SIZE_GB = SIZE_MB * 1024L;
	private static final int SCALE = 2;

	public static void main(String[] argv) {
		long longSize = 12312342;
		System.out.println(longSize + " 代表文件大小是:" + toStringSize(longSize));
	}

	private static String toStringSize(Long longSize) {
		if (longSize >= SIZE_BT && longSize < SIZE_KB) {
			return longSize / SIZE_BT + "KB";
		} else if (longSize >= SIZE_KB && longSize < SIZE_MB) {
			return longSize / SIZE_KB + "MB";
		} else if (longSize >= SIZE_MB && longSize < SIZE_GB) {
			BigDecimal longs = new BigDecimal(Double.valueOf(longSize + "").toString());
			BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_MB + "").toString());
			String result = longs.divide(sizeMB, SCALE, BigDecimal.ROUND_HALF_UP).toString();
			return result + "GB";
		} else if (longSize >= SIZE_GB) {
			BigDecimal longs = new BigDecimal(Double.valueOf(longSize + "").toString());
			BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_GB + "").toString());
			String result = longs.divide(sizeMB, SCALE, BigDecimal.ROUND_HALF_UP).toString();
			return result + "TB";
		} else if (longSize < 0) {
			return "0B";
		} else {
			return longSize + "B";
		}
	}
}
